/*****************************
*   brA Convert TXT to SQL
*   Autor: Shiraz
*	Data: 22/10/2012
******************************/

#include "brA_convert.h"

int main(void)
{
	show_message(GREEN, "                                                                               "NEWLINE);
	show_message(GREEN, "                 _           ___  _   _                                        "NEWLINE);
	show_message(GREEN, "                | |         / _ \\| | | |                                       "NEWLINE);
	show_message(GREEN, "                | |__  _ __/ /_\\ \\ |_| |__   ___ _ __    _ _                   "NEWLINE);
	show_message(GREEN, "                | '_ \\| '__|  _  | __| '_ \\ / _ \\ '_ \\ / _` |                  "NEWLINE);
	show_message(GREEN, "                | |_) | |  | | | | |_| | | |  __/ | | | (_| |                  "NEWLINE);
	show_message(GREEN, "                |_.__/|_|  \\_| |_/\\__|_| |_|\\___|_| |_|\\__,_|                  "NEWLINE);
	show_message(GREEN, "                                                                               "NEWLINE);
	show_message(GREEN, "                      Project brAthena (c) 2008 - %s                         "NEWLINE, YEAR);
	show_message(GREEN, "                              www.brathena.org                                 "NEWLINE);
	show_message(GREEN, "                                                                               "NEWLINE NEWLINE);
	
	init_system();

	convert_abra_db();
	convert_castle_db();
	convert_create_arrow_db();
	convert_elemental_db();
	convert_elemental_skill_db();
	convert_guild_skill_tree_db();
	convert_homunculus_db();
	convert_homun_skill_tree_db();
	convert_item_avail_db();
	convert_item_delay_db();
	convert_item_findingore_db();
	convert_item_stack_db();
	convert_magicmushroom_db();
	convert_mercenary_db();
	convert_mercenary_skill_db();
	convert_mob_avail_db();
	convert_mob_chat_db();
	convert_mob_classchange_db();
	convert_mob_item_ratio_db();
	convert_mob_pouch_db();
	convert_pet_db();
	convert_produce_db();
	convert_skill_changematerial_db();
	convert_skill_improvise_db();
	convert_skill_reproduce_db();
	convert_spellbook_db();
	convert_quest_db();
	convert_size_fix_db();
	convert_item_bluebox_db();
	convert_item_buyingstore_db();
	convert_item_cardalbum_db();
	convert_item_combo_db();
	convert_item_giftbox_db();
	convert_item_misc_db();
	convert_item_noequip_db();
	convert_item_trade_db();
	convert_item_violetbox_db();
	convert_mob_boss_db();
	convert_mob_branch_db();
	convert_mob_poring_db();
	convert_mob_race2_db();
	convert_skill_cast_db();
	convert_skill_castnodex_db();
	convert_skill_db();
	convert_skill_nocast_db();
	convert_skill_require_db();
	convert_skill_tree_db();
	convert_skill_unit_db();
	convert_exp_guild_db();
	convert_exp_homun_db();
	convert_statpoint_db();
	convert_refine_db();
	convert_mob_db();
	convert_mob_skill_db();
	convert_item_db();
	
	show_message(YELLOW, "Converted files: %d\n", file_count);
	abort_system();
}

void init_system(void)
{
	char line[HBUFFER], path[LBUFFER];
	int i;
	FILE *fp;
	
	memset(item_data,  0, sizeof(item_data));
	memset(mob_data,   0, sizeof(mob_data));
	memset(skill_data, 0, sizeof(skill_data));

	if(!(fp = fopen(CONFIG_FILE, "r"))) {
		show_message(RED, "File not found:% s. stopping..."NEWLINE, CONFIG_FILE);
		abort_system();
	}
	
	while(fgets(line, sizeof(line), fp)) {
		line[strlen(line)-1] = '\0';
		char **rows, *str = line;

		if(strstr(line, "[") || strstr(line, ";"))
			continue;
		
		explode(&rows, str, '=');
		
		if(!strcmp(rows[0], "host"))
			strncpy(config.host, rows[1], sizeof(config.host));
		else if(!strcmp(rows[0], "user"))
			strncpy(config.user, rows[1], sizeof(config.user));
		else if(!strcmp(rows[0], "password"))
			strncpy(config.password, rows[1], sizeof(config.password));
		else if(!strcmp(rows[0], "database"))
			strncpy(config.database, rows[1], sizeof(config.database));
		else if(!strcmp(rows[0], "version"))
			strncpy(config.version, rows[1], sizeof(config.version));
	}
	
	mysqlPtr = mysql_init(NULL);

	if(!(mysql_real_connect(mysqlPtr, config.host, config.user, config.password, config.database, 0, NULL, 0))) {
		show_message(RED, "An error occurred in connection with the MySQL: %s"NEWLINE, mysql_error(mysqlPtr));
		abort_system();
	}
	
	show_message(WHITE, "Connection to MySQL: OK"NEWLINE);
	
	sprintf(path, "%s/%s", DIR(2), "item_db.dat");
	
	if((fp = fopen(path, "rb"))) {
		while(fgets(line, sizeof(line), fp) != NULL) {
			file_dat(line, 0xFACA);
			char **rows, *str = line;

			if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
				continue;

			explode(&rows, str, '#');
		
			for(i = 0; i < 10; ++i)
				rows[1] = replace_str(rows[1], "_", " ");
		
			strncpy(item_data[atoi(rows[0])].item_name, rows[1], sizeof((*item_data).item_name));
		}
	}
	
	sprintf(path, "%s/%s", DIR(2), "skill_db.dat");
	
	if((fp = fopen(path, "rb"))) {
		while(fgets(line, sizeof(line), fp) != NULL) {
			file_dat(line, 0xFACA);
			char **rows, *str;

			if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
				continue;

			line[strlen(line)-1] = '\0';	
			str = line;
			explode(&rows, str, ',');
		
			strncpy(skill_data[atoi(rows[0])].skill_name, rows[16], sizeof((*skill_data).skill_name));
		}
	}
	
	sprintf(path, "%s/%s", DIR(2), "mob_db.dat");
	
	if((fp = fopen(path, "rb"))) {
		while(fgets(line, sizeof(line), fp) != NULL) {
			file_dat(line, 0xFACA);
			char **rows, *str;

			if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
				continue;

			line[strlen(line)-1] = '\0';	
			str = line;
			explode(&rows, str, ',');
		
			strncpy(mob_data[atoi(rows[0])].mob_name, rows[3], sizeof((*mob_data).mob_name));
		}
	}
	
	show_message(YELLOW, "Converting files, wait a few minutes.."NEWLINE);
	fclose(fp);
}

void abort_system(void)
{
	memset(item_data,  0, sizeof(item_data));
	memset(mob_data,   0, sizeof(mob_data));
	memset(skill_data, 0, sizeof(skill_data));
	
	mysql_close(mysqlPtr);
	
	system("PAUSE");
	exit(0);
}

void show_message(int color, const char *msg, ...)
{
	TXTCOLOR(color);
	va_list args;

	va_start(args, msg);
	vfprintf(stdout, msg, args);
	TXTCOLOR(DEFAULT);
}

int exec_mysql_query(const char* query)
{
	if(mysql_query(mysqlPtr, query)) {
		show_message(RED, "An error occurred during query: %s"NEWLINE, mysql_error(mysqlPtr));
		abort_system();
	}

	return 0;
}

const char *getitemname(int item_id, char *item_name)
{
	if(!strlen(item_data[item_id].item_name))
		return escape_str(item_name);

	return escape_str(item_data[item_id].item_name);
}

const char *getskillname(int skill_id, char *skill_name)
{
	if(!strlen(skill_data[skill_id].skill_name))
		return escape_str(skill_name);

	return escape_str(skill_data[skill_id].skill_name);
}

const char *getmobname(int mob_id, char *mob_name)
{
	if(!strlen(mob_data[mob_id].mob_name))
		return escape_str(mob_name);

	return escape_str(mob_data[mob_id].mob_name);
}

char *escape_str(char *str)
{
    char *new_str;
    char *source, *target;
    char *end;
    int new_length;
    int length = strlen(str);

    if(length == 0) {
        char *p = (char *) malloc(1);
        if (p == NULL) {
            return p;
        }
        p[0] = 0;
        return p;
    }

    new_str = (char *) malloc(2 * length + 1);
    if(new_str == NULL) {
        return new_str;
    }
    source = str;
    end = source + length;
    target = new_str;
    
    while(source < end) {
        switch(*source) {
            case '\0':
                *target++ = '\\';
                *target++ = '0';
                break;
            case '\'':
            case '\"':
            case '\\':
                *target++ = '\\';
            default:
                *target++ = *source;
                break;
        }
        source++;
    }
    
    *target = 0;
    new_length = target - new_str;
    new_str = (char *) realloc(new_str, new_length + 1);
    return new_str;
}

int explode(char ***arr_ptr, char *str, char delimiter)
{
	char *src = str, *end, *dst;
	char **arr;
	int size = 1, i;

	while((end = strchr(src, delimiter)) != NULL) {
		++size;
		src = end + 1;
	}

	arr = malloc(size * sizeof(char *) + (strlen(str) + 1) * sizeof(char));

	src = str;
	dst = (char *) arr + size * sizeof(char *);
	for(i = 0; i < size; ++i) {
		if ((end = strchr(src, delimiter)) == NULL)
			end = src + strlen(src);
		arr[i] = dst;
		strncpy(dst, src, end - src);
		dst[end - src] = '\0';
		dst += end - src + 1;
		src = end + 1;
	}
	*arr_ptr = arr;

	return size;
}

char *replace_str(char *str, char *orig, char *rep)
{
	static char buffer[4096];
	char *p;
  
	if(!(p = strstr(str, orig)))
		return str;

	strncpy(buffer, str, p-str);
	buffer[p-str] = '\0';

	sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
	return buffer;
}

void file_dat(char str[],int key)
{
    unsigned int i;
	
    for(i = 0; i < strlen(str); ++i)
          str[i] = str[i] + key;
}

int count_char(char *str, char c)
{
    int count = 0;
    char *p;
	
    for(p = str; *p; p++)
        if(*p == c)
            count++;
			
    return count;
}

void convert_abra_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "abra_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[3];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 4; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO abra_db VALUES(%s%,'%s',%s,%s)", rows[0], getskillname(atoi(rows[0]), rows[1]), rows[2], rows[3]);
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_castle_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "castle_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;

	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[4];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 5; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO castle_db VALUES(%s,'%s','%s','%s',%d)", rows[0], escape_str(rows[1]), escape_str(rows[2]), escape_str(rows[3]), atoi(rows[4]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_create_arrow_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "create_arrow_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[10];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 11; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO create_arrow_db VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", (!atoi(rows[0])?"NULL":rows[0]), (!atoi(rows[1])?"NULL":rows[1]), (!atoi(rows[2])?"NULL":rows[2]), (!atoi(rows[3])?"NULL":rows[3]), (!atoi(rows[4])?"NULL":rows[4]), (!atoi(rows[5])?"NULL":rows[5]), (!atoi(rows[6])?"NULL":rows[6]), (!atoi(rows[7])?"NULL":rows[7]), (!atoi(rows[8])?"NULL":rows[8]), (!atoi(rows[9])?"NULL":rows[9]), (!atoi(rows[10])?"NULL":rows[10]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_elemental_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "elemental_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[25];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 26; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO elemental_db VALUES(%s,'%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", 
		rows[0], escape_str(rows[1]), escape_str(rows[2]), rows[3], rows[4], rows[5], rows[6], rows[7], rows[8], rows[9], rows[10], rows[11],
		rows[12], rows[13], rows[14], rows[15], rows[16], rows[17], rows[18], rows[19], rows[20], rows[21], rows[22], rows[23], rows[24], rows[25]);
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_elemental_skill_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "elemental_skill_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[3];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 4; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO elemental_skill_db VALUES(%s,%s,%s,%d)", rows[0], rows[1], rows[2], atoi(rows[3]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_guild_skill_tree_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "guild_skill_tree.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[11];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 12; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO guild_skill_tree_db VALUES(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_homunculus_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "homunculus_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[49];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 50; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO homunculus_db VALUES(%d,%d,'%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), atoi(rows[1]), escape_str(rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]),
		atoi(rows[12]), atoi(rows[13]), atoi(rows[14]), atoi(rows[15]), atoi(rows[16]), atoi(rows[17]), atoi(rows[18]), atoi(rows[19]), atoi(rows[20]), atoi(rows[21]), atoi(rows[22]), atoi(rows[23]), atoi(rows[24]), atoi(rows[25]),
		atoi(rows[26]), atoi(rows[27]), atoi(rows[28]), atoi(rows[29]), atoi(rows[30]), atoi(rows[31]), atoi(rows[32]), atoi(rows[33]), atoi(rows[34]), atoi(rows[35]), atoi(rows[36]), atoi(rows[37]), atoi(rows[38]), atoi(rows[39]),
		atoi(rows[40]), atoi(rows[41]), atoi(rows[42]), atoi(rows[43]), atoi(rows[44]), atoi(rows[45]), atoi(rows[46]), atoi(rows[47]), atoi(rows[48]), atoi(rows[49]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_homun_skill_tree_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "homun_skill_tree.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[13];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 14; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO homun_skill_tree_db VALUES(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]), atoi(rows[12]), atoi(rows[13]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_avail_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_avail.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_avail_db VALUES(%d,%d)", atoi(rows[0]), atoi(rows[1]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_delay_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_delay.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_delay_db VALUES(%d,%d)", atoi(rows[0]), atoi(rows[1]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_findingore_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_findingore.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[3];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 4; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_findingore_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_stack_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_stack.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[3];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 4; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_stack_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_magicmushroom_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "magicmushroom_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[1];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 2; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO magicmushroom_db VALUES(%d)", atoi(rows[0]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mercenary_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mercenary_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[25];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 26; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mercenary_db VALUES(%d,'%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), escape_str(rows[1]), getmobname(atoi(rows[0]), rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]),
		atoi(rows[12]), atoi(rows[13]), atoi(rows[14]), atoi(rows[15]), atoi(rows[16]), atoi(rows[17]), atoi(rows[18]), atoi(rows[19]), atoi(rows[20]), atoi(rows[21]), atoi(rows[22]), atoi(rows[23]), atoi(rows[24]), atoi(rows[25]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mercenary_skill_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mercenary_skill_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[3];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 4; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mercenary_skill_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_avail_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_avail.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[11];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 12; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_avail_db VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", 
		rows[0], (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]), (rows[3]==NULL?"NULL":rows[3]), (rows[4]==NULL?"NULL":rows[4]), (rows[5]==NULL?"NULL":rows[5]), (rows[6]==NULL?"NULL":rows[6]), (rows[7]==NULL?"NULL":rows[7]), (rows[8]==NULL?"NULL":rows[8]), (rows[9]==NULL?"NULL":rows[9]), (rows[10]==NULL?"NULL":rows[10]), (rows[11]==NULL?"NULL":rows[11]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_chat_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_chat_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_chat_db VALUES(%d,'%s','%s')", atoi(rows[0]), escape_str(rows[1]), escape_str(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_classchange_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_classchange.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_classchange_db VALUES(%d,'%s',%d)", atoi(rows[0]), getmobname(atoi(rows[0]), rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_item_ratio_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_item_ratio.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_item_ratio_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_pouch_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_pouch.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_pouch_db VALUES(%d,'%s',%d)", atoi(rows[0]), getmobname(atoi(rows[0]), rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_pet_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0;
	
	sprintf(path, "%s/%s", DIR(1), "pet_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char **rows, **rows2, *str = line, script1[256], script2[256];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
			
		explode(&rows, str, ',');
		explode(&rows2, str, '{');
		
		sprintf(script1, "{ %s", replace_str(rows2[1], "},", "}"));
		sprintf(script2, "{%s", replace_str(rows2[2], "// kRO version", ""));

		sprintf(write, "REPLACE INTO pet_db VALUES(%s,'%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'%s','%s')", 
		rows[0], escape_str(rows[1]), getmobname(atoi(rows[0]), rows[2]), rows[3], rows[4], rows[5], rows[6], rows[7], rows[8], rows[9], rows[10], rows[11],
		rows[12], rows[13], rows[14], rows[15], rows[16], rows[17], rows[18], rows[19], escape_str(script1), escape_str(replace_str(script2, "// iRO/cRO version", "")));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_produce_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "produce_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[27];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 28; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO produce_db VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", 
		(rows[0]==NULL?"NULL":rows[0]), (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]), (rows[3]==NULL?"NULL":rows[3]), (rows[4]==NULL?"NULL":rows[4]), (rows[5]==NULL?"NULL":rows[5]), (rows[6]==NULL?"NULL":rows[6]), (rows[7]==NULL?"NULL":rows[7]), (rows[8]==NULL?"NULL":rows[8]), (rows[9]==NULL?"NULL":rows[9]), (rows[10]==NULL?"NULL":rows[10]), (rows[11]==NULL?"NULL":rows[11]),
		(rows[12]==NULL?"NULL":rows[12]), (rows[13]==NULL?"NULL":rows[13]), (rows[14]==NULL?"NULL":rows[14]), (rows[15]==NULL?"NULL":rows[15]), (rows[16]==NULL?"NULL":rows[16]), (rows[17]==NULL?"NULL":rows[17]), (rows[18]==NULL?"NULL":rows[18]), (rows[19]==NULL?"NULL":rows[19]), (rows[20]==NULL?"NULL":rows[20]), (rows[21]==NULL?"NULL":rows[21]), (rows[22]==NULL?"NULL":rows[22]), (rows[23]==NULL?"NULL":rows[23]), (rows[24]==NULL?"NULL":rows[24]),
		(rows[25]==NULL?"NULL":rows[25]), (rows[26]==NULL?"NULL":rows[26]), (rows[27]==NULL?"NULL":rows[27]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_changematerial_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_changematerial_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[11];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 12; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_changematerial_db VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
		(rows[0]==NULL?"NULL":rows[0]), (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]), (rows[3]==NULL?"NULL":rows[3]), (rows[4]==NULL?"NULL":rows[4]), (rows[5]==NULL?"NULL":rows[5]), (rows[6]==NULL?"NULL":rows[6]), (rows[7]==NULL?"NULL":rows[7]), (rows[8]==NULL?"NULL":rows[8]), (rows[9]==NULL?"NULL":rows[9]), (rows[10]==NULL?"NULL":rows[10]), (rows[11]==NULL?"NULL":rows[11]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_improvise_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_improvise_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_improvise_db VALUES(%d,%d)", atoi(rows[0]), atoi(rows[1]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_reproduce_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_reproduce_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[1];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 2; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_reproduce_db VALUES(%d)", atoi(rows[0]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_spellbook_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "spellbook_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO spellbook_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_quest_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "quest_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[8];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 9; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO quest_db VALUES(%d,%d,%d,%d,%d,%d,%d,%d,'%s')", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), escape_str(rows[8]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_size_fix_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "size_fix.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[23];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 24; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO size_fix_db VALUES(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]),
		atoi(rows[12]), atoi(rows[13]), atoi(rows[14]), atoi(rows[15]), atoi(rows[16]), atoi(rows[17]), atoi(rows[18]), atoi(rows[19]), atoi(rows[20]), atoi(rows[21]), atoi(rows[22]), atoi(rows[23]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_bluebox_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_bluebox.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_bluebox_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_buyingstore_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_buyingstore.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[1];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 2; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_buyingstore_db VALUES(%d)", atoi(rows[0]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_cardalbum_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_cardalbum.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_cardalbum_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_combo_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_combo_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *rows[3], *p;

		if(line[0] == '/' && line[1] == '/')
			continue;
			
		memset(rows, 0, sizeof(rows));
		p = line;
		
		while(isspace(*p))
			++p;
			
		if(*p == '\0')
			continue;
			
		for(i = 0; i < 1; ++i) {
			rows[i] = p;
			p = strchr(p,',');
			
			if(p == NULL)
				break;
				
			*p = '\0';
			p += 1;
		}
			
		rows[1] = p;
	
		sprintf(write, "REPLACE INTO item_combo_db VALUES('%s','%s')", escape_str(rows[0]), escape_str(rows[1]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_giftbox_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_giftbox.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_giftbox_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_misc_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_misc.txt");
	
	if(!(fp = fopen(path, "r")))
		return;

	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_misc_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_noequip_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_noequip.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_noequip_db VALUES(%d,%d)", atoi(rows[0]), atoi(rows[1]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_trade_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_trade.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_trade_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_violetbox_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "item_violetbox.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO item_violetbox_db VALUES(%d,%d,%d)", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_boss_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_boss.txt");
	
	if(!(fp = fopen(path, "r")))
		return;

	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_boss_db VALUES(%d,'%s',%d)", atoi(rows[0]), getmobname(atoi(rows[0]), rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_branch_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_branch.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_branch_db VALUES(%d,'%s',%d)", atoi(rows[0]), getmobname(atoi(rows[0]), rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_poring_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_poring.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_poring_db VALUES(%d,'%s',%d)", atoi(rows[0]), getmobname(atoi(rows[0]), rows[1]), atoi(rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_race2_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "mob_race2_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[9];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 10; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO mob_race2_db VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", 
		(rows[0]==NULL?"NULL":rows[0]), (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]), (rows[3]==NULL?"NULL":rows[3]), (rows[4]==NULL?"NULL":rows[4]), (rows[5]==NULL?"NULL":rows[5]), (rows[6]==NULL?"NULL":rows[6]), (rows[7]==NULL?"NULL":rows[7]), (rows[8]==NULL?"NULL":rows[8]), (rows[9]==NULL?"NULL":rows[9]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_cast_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i, version;
	
	sprintf(path, "%s/%s", DIR(1), "skill_cast_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	if(!strcmp(config.version,"re")) version = 8; else version = 7;
	
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[7];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < version; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}

		if(version == 8) sprintf(write, "REPLACE INTO skill_cast_db VALUES(%s,'%s','%s','%s','%s','%s','%s','%s')", (rows[0]==NULL?"NULL":rows[0]), (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]), (rows[3]==NULL?"NULL":rows[3]), (rows[4]==NULL?"NULL":rows[4]), (rows[5]==NULL?"NULL":rows[5]), (rows[6]==NULL?"NULL":rows[6]), (rows[7]==NULL?"NULL":rows[7])); else sprintf(write, "REPLACE INTO skill_cast_db VALUES(%s,'%s','%s','%s','%s','%s','%s')", (rows[0]==NULL?"NULL":rows[0]), (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]), (rows[3]==NULL?"NULL":rows[3]), (rows[4]==NULL?"NULL":rows[4]), (rows[5]==NULL?"NULL":rows[5]), (rows[6]==NULL?"NULL":rows[6]));
		exec_mysql_query(write);
		count++;
	}

	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_castnodex_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_castnodex_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_castnodex_db VALUES(%s,%s,%s)", (rows[0]==NULL?"NULL":rows[0]), (rows[1]==NULL?"NULL":rows[1]), (rows[2]==NULL?"NULL":rows[2]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i, k;
	
	sprintf(path, "%s/%s", DIR(1), "skill_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char **rows, *str = line;

		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;

		explode(&rows, str, ',');
		
		for(k = 0; k < 10; ++k)
			rows[15] = replace_str(rows[15], "	", "");
		
		sprintf(write, "REPLACE INTO skill_db VALUES(%s,'%s',%s,%s,'%s','%s','%s',%s,'%s','%s',%s,'%s','%s','%s','%s','%s','%s')",
		rows[0], escape_str(rows[1]), rows[2], rows[3], rows[4], escape_str(rows[5]), escape_str(rows[6]), rows[7], escape_str(rows[8]), escape_str(rows[9]), rows[10],
		escape_str(rows[11]), escape_str(rows[12]), escape_str(rows[13]), escape_str(rows[14]), escape_str(rows[15]), getskillname(atoi(rows[0]), rows[16]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_nocast_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_nocast_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[2];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 3; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_nocast_db VALUES(%d,%d)", atoi(rows[0]), atoi(rows[1]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_require_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_require_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[31];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 32; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_require_db VALUES(%d,'%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
		atoi(rows[0]), escape_str(rows[1]), escape_str(rows[2]), escape_str(rows[3]), escape_str(rows[4]), escape_str(rows[5]), atoi(rows[6]), escape_str(rows[7]), atoi(rows[8]), escape_str(rows[9]), escape_str(rows[10]),
		escape_str(rows[11]), atoi(rows[12]), atoi(rows[13]), atoi(rows[14]), atoi(rows[15]), atoi(rows[16]), atoi(rows[17]), atoi(rows[18]), atoi(rows[19]), atoi(rows[20]), atoi(rows[21]), atoi(rows[22]), atoi(rows[23]), atoi(rows[24]),
		atoi(rows[25]), atoi(rows[26]), atoi(rows[27]), atoi(rows[28]), atoi(rows[29]), atoi(rows[30]), atoi(rows[31]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_tree_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "skill_tree.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[12];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 13; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO skill_tree_db VALUES(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]), atoi(rows[12]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_skill_unit_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i, k;
	
	sprintf(path, "%s/%s", DIR(1), "skill_unit_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char* token;
		char *rows[7], **last, s1[1024];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
			
		line[strlen(line)-1] = '\0';
		token = strtok(line, ",");
		
		for(i = 0; i < 8; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		for(k = 0; k < 20; ++k)
			rows[2] = replace_str(rows[2], " ", "");
		
		explode(&last, rows[7], '/');
		sprintf(s1, "'%s'", rows[2]);
		
		sprintf(write, "REPLACE INTO skill_unit_db VALUES(%d,'%s',%s,'%s','%s','%s','%s','%s')", 
		atoi(rows[0]), escape_str(rows[1]), (!strlen(rows[2])?NULL:s1), escape_str(rows[3]), escape_str(rows[4]), escape_str(rows[5]), escape_str(rows[6]), escape_str(replace_str(last[0], "  ", "")));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_exp_guild_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "exp_guild.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[1];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 2; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO exp_guild_db VALUES(%d)", atoi(rows[0]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_exp_homun_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "exp_homun.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[1];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 2; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO exp_homun_db VALUES(%d)", atoi(rows[0]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_statpoint_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	
	sprintf(path, "%s/%s", DIR(1), "statpoint.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[1];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 2; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		sprintf(write, "REPLACE INTO statpoint_db VALUES(%d)", atoi(rows[0]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_refine_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i, version;
	
	sprintf(path, "%s/%s", DIR(1), "refine_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	if(!strcmp(config.version, "re")) version = 24; else version = 14;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[23];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < version; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		if(version == 24) 
			sprintf(write, "REPLACE INTO refine_db VALUES(%d,%d,%d,%d,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), escape_str(rows[4]), escape_str(rows[5]), escape_str(rows[6]), escape_str(rows[7]), escape_str(rows[8]), escape_str(rows[9]), escape_str(rows[10]), escape_str(rows[11]), escape_str(rows[12]), escape_str(rows[13]), escape_str(rows[14]), escape_str(rows[15]), escape_str(rows[16]), escape_str(rows[17]), escape_str(rows[18]), escape_str(rows[19]), escape_str(rows[20]), escape_str(rows[21]), escape_str(rows[22]), escape_str(rows[23])); else sprintf(write, "REPLACE INTO refine_db VALUES(%d,%d,%d,%d,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", atoi(rows[0]), atoi(rows[1]), atoi(rows[2]), atoi(rows[3]), escape_str(rows[4]), escape_str(rows[5]), escape_str(rows[6]), escape_str(rows[7]), escape_str(rows[8]), escape_str(rows[9]), escape_str(rows[10]), escape_str(rows[11]), escape_str(rows[12]), escape_str(rows[13]));
			
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;

	sprintf(path, "%s/%s", DIR(1), "mob_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *token = strtok(line, ",");
		char *rows[56];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		for(i = 0; i < 57; ++i) {
			rows[i] = token;
			token = strtok(0, ",");
		}
		
		if(!strlen(rows[25]))
			rows[25] = 0;
		
		sprintf(write, "REPLACE INTO mob_db VALUES(%d,'%s','%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 
		atoi(rows[0]), escape_str(rows[1]), getmobname(atoi(rows[0]), rows[2]), getmobname(atoi(rows[0]), rows[3]), atoi(rows[4]), atoi(rows[5]), atoi(rows[6]), atoi(rows[7]), atoi(rows[8]), atoi(rows[9]), atoi(rows[10]), atoi(rows[11]),
		atoi(rows[12]), atoi(rows[13]), atoi(rows[14]), atoi(rows[15]), atoi(rows[16]), atoi(rows[17]), atoi(rows[18]), atoi(rows[19]), atoi(rows[20]), atoi(rows[21]), atoi(rows[22]), atoi(rows[23]),
		atoi(rows[24]), escape_str(rows[25]), atoi(rows[26]), atoi(rows[27]), atoi(rows[28]), atoi(rows[29]), atoi(rows[30]), atoi(rows[31]), atoi(rows[32]), atoi(rows[33]), atoi(rows[34]), atoi(rows[35]), atoi(rows[36]),
		atoi(rows[37]), atoi(rows[38]), atoi(rows[39]), atoi(rows[40]), atoi(rows[41]), atoi(rows[42]), atoi(rows[43]), atoi(rows[44]), atoi(rows[45]), atoi(rows[46]), atoi(rows[47]), atoi(rows[48]), atoi(rows[49]),
		atoi(rows[50]), atoi(rows[51]), atoi(rows[52]), atoi(rows[53]), atoi(rows[54]), atoi(rows[55]), atoi(rows[56]));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_mob_skill_db()
{
	FILE *fp;
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;

	sprintf(path, "%s/%s", DIR(1), "mob_skill_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *rows[32], *p, col1[4024], col2[4024], col8[4024], col9[4024], col10[4024], col11[4024], col17[4024], col18[4024];
		
		if((line[0] == '/' && line[1] == '/') || line[0] == '\n')
			continue;
		
		memset(rows, 0, sizeof(rows));

		p = line;
		while( isspace(*p) )
			++p;
		if( *p == '\0' )
			continue;
		for( i = 0; i < 19; ++i ) {
			rows[i] = p;
			p = strchr(p,',');
			if( p == NULL )
				break;
			*p = '\0';
			++p;
		}

		sprintf(col1,  "'%s'", escape_str(rows[1]));
		sprintf(col2,  "'%s'", escape_str(rows[2]));
		sprintf(col8,  "'%s'", escape_str(rows[8]));
		sprintf(col9,  "'%s'", escape_str(rows[9]));
		sprintf(col10, "'%s'", escape_str(rows[10]));
		sprintf(col11, "'%s'", escape_str(rows[11]));
		sprintf(col17, "'%s'", escape_str(rows[17]));
		sprintf(col18, "'%s'", escape_str(rows[18]));		
		
		sprintf(write, "REPLACE INTO mob_skill_db VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", 
		rows[0], (!strlen(rows[1])?NULL:col1), (!strlen(rows[2])?NULL:col2), (!strlen(rows[3])?NULL:rows[3]), (!strlen(rows[4])?NULL:rows[4]), (!strlen(rows[5])?NULL:rows[5]), (!strlen(rows[6])?NULL:rows[6]), (!strlen(rows[7])?NULL:rows[7]), (!strlen(rows[8])?NULL:col8), (!strlen(rows[9])?NULL:col9), (!strlen(rows[10])?NULL:col10), (!strlen(rows[11])?NULL:col11),
		(!strlen(rows[12])?NULL:rows[12]), (!strlen(rows[13])?NULL:rows[13]), (!strlen(rows[14])?NULL:rows[14]), (!strlen(rows[15])?NULL:rows[15]), (!strlen(rows[16])?NULL:rows[16]), (!strlen(rows[17])?NULL:col17), (!strlen(rows[18])?NULL:col18));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}

void convert_item_db()
{
	char line[HBUFFER], write[HBUFFER], path[LBUFFER];
	int count = 0, i;
	FILE *fp;

	sprintf(path, "%s/%s", DIR(1), "item_db.txt");
	
	if(!(fp = fopen(path, "r")))
		return;
	
	while(fgets(line, sizeof(line), fp) != NULL) {
		char *rows[32], *p, script1[1024], script2[1024], script3[1024], atkre[1024];

		if(line[0] == '/' && line[1] == '/')
			continue;
		
		memset(rows, 0, sizeof(rows));

		p = line;
		while( isspace(*p) )
			++p;
		if( *p == '\0' )
			continue;
		for( i = 0; i < 19; ++i ) {
			rows[i] = p;
			p = strchr(p,',');
			if( p == NULL )
				break;
			*p = '\0';
			++p;
		}

		p[0] = ' '; rows[19] = p; p = strstr(p+1,"},"); p[0] = ' '; p[1] = '\0'; p += 2; p[0] = ' '; rows[20] = p; p = strstr(p+1,"},"); p[0] = ' '; p[1] = '\0'; p += 2; p[0] = ' '; rows[21] = p; p = strrchr(p+1, '}'); p[0] = ' ';
		
		sprintf(script1, "'%s'", escape_str(rows[19]));
		sprintf(script2, "'%s'", escape_str(rows[20]));
		sprintf(script3, "'%s'", escape_str(rows[21]));
		sprintf(atkre,   "'%s'", escape_str(rows[7]));

		sprintf(write, "REPLACE INTO item_db VALUES(%s, '%s', '%s', '%s', %s, %s, '%s', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", 
		rows[0], escape_str(rows[1]), getitemname(atoi(rows[0]), rows[2]), escape_str(rows[3]), (!strlen(rows[4])?NULL:rows[4]), (!strlen(rows[5])?NULL:rows[5]), escape_str(rows[6]), (!strlen(rows[7])?NULL:atkre), (!strlen(rows[8])?NULL:rows[8]), (!strlen(rows[9])?NULL:rows[9]), (!strlen(rows[10])?NULL:rows[10]), (!strlen(rows[11])?NULL:rows[11]), (!strlen(rows[12])?NULL:rows[12]), (!strlen(rows[13])?NULL:rows[13]), (!strlen(rows[14])?NULL:rows[14]), (!strlen(rows[15])?NULL:rows[15]), (!strlen(rows[16])?NULL:rows[16]), (!strlen(rows[17])?NULL:rows[17]), (!strlen(rows[18])?NULL:rows[18]), (strlen(rows[19]) < 3 ? NULL : script1), (strlen(rows[20]) < 3 ? NULL : script2), (strlen(rows[21]) < 4 ? NULL : script3));
		exec_mysql_query(write);
		count++;
	}
	
	show_message(LIGHT_GREEN, "File %s successfully converted! rows affected: %d"NEWLINE, path, count);
	file_count++;
	fclose(fp);
}
